/**
 * Copyright 2015-2016 org.lychee.com 
 * All rights reserved.
 *
 * @project
 * @author pro
 * @version 1.0
 * @date 2016-03-07
 */
package com.eyaomall.wxmall.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;
import com.eyaomall.wxmall.common.error.BECode;
import com.eyaomall.wxmall.common.helper.MemberHelper;
import com.eyaomall.wxmall.common.helper.SchedulerHelper;
import com.eyaomall.wxmall.common.util.DateUtils;
import com.eyaomall.wxmall.dao.DepositDao;
import com.eyaomall.wxmall.dao.DiscountRecordDao;
import com.eyaomall.wxmall.dao.EmployeeDao;
import com.eyaomall.wxmall.dao.EmployeeDepositDao;
import com.eyaomall.wxmall.dao.GoodsDao;
import com.eyaomall.wxmall.dao.GoodsTransportDao;
import com.eyaomall.wxmall.dao.OrderDao;
import com.eyaomall.wxmall.dao.OrderGoodsDao;
import com.eyaomall.wxmall.dao.ShopExtDao;
import com.eyaomall.wxmall.dao.ShoppingChartDao;
import com.eyaomall.wxmall.dao.WxMemberDao;
import com.eyaomall.wxmall.model.po.Address;
import com.eyaomall.wxmall.model.po.Coupon;
import com.eyaomall.wxmall.model.po.CouponGoods;
import com.eyaomall.wxmall.model.po.Deposit;
import com.eyaomall.wxmall.model.po.Employee;
import com.eyaomall.wxmall.model.po.EmployeeDeposit;
import com.eyaomall.wxmall.model.po.Goods;
import com.eyaomall.wxmall.model.po.Member;
import com.eyaomall.wxmall.model.po.Order;
import com.eyaomall.wxmall.model.po.OrderGoods;
import com.eyaomall.wxmall.model.po.Shop;
import com.eyaomall.wxmall.model.po.ShopExt;
import com.eyaomall.wxmall.model.po.ShoppingChart;
import com.eyaomall.wxmall.model.po.WxMember;
import com.eyaomall.wxmall.model.pojo.ReqGoods;
import com.eyaomall.wxmall.model.vo.DiscountRecordVO;
import com.eyaomall.wxmall.model.vo.OrderVO;
import com.eyaomall.wxmall.model.vo.TranspsortVO;
import com.eyaomall.wxmall.service.CouponService;
import com.eyaomall.wxmall.service.MemberService;
import com.eyaomall.wxmall.service.OrderService;
import com.github.pagehelper.StringUtil;
import com.lycheeframework.core.cmp.kit.IHTTPObject;
import com.lycheeframework.core.cmp.kit.Pages;
import com.lycheeframework.core.common.helper.GlobEnv;
import com.lycheeframework.core.common.util.DateUtil;
import com.lycheeframework.core.exception.ErrorCodeException;
import com.lycheeframework.core.support.ServiceSupport;

/**
 * 订单service实现
 * 
 * @author pro
 *
 */
@Service
public class OrderServiceImpl extends ServiceSupport implements OrderService {

	@Autowired
	private OrderDao orderDao;

	@Autowired
	private GoodsDao goodsDao;

	@Autowired
	private EmployeeDao employeeDao;

	@Autowired
	private EmployeeDepositDao employeeDepositDao;

	@Autowired
	private OrderGoodsDao orderGoodsDao;

	@Autowired
	private ShopExtDao shopExtDao;

	@Autowired
	private MemberService memberService;

	@Autowired
	private GoodsTransportDao transportDao;

	@Autowired
	private DiscountRecordDao discountRecordDao;

	@Autowired
	private ShoppingChartDao shoppingChartDao;

	@Autowired
	private WxMemberDao wxMemberDao;

	@Autowired
	private DepositDao depositDao;
	
	@Autowired
	private CouponService couponService;

	private final static Logger LOG = LoggerFactory.getLogger(OrderServiceImpl.class);

	/**
	 * 新增我的订单
	 */
	public Integer create(Order order) {

		return orderDao.create(order);
	}

	/**
	 * 修改订单
	 */
	public boolean update(Order order) {

		return orderDao.update(order);
	}

	/**
	 * 根据订单ID查询我的订单详情
	 */
	public OrderVO queryById(Integer orderId) {
		OrderVO orderVO = new OrderVO();
		orderVO.setId(orderId);

		return orderDao.query(orderVO);
	}

	/**
	 * 根据订单编号查询我的订单详情
	 */
	public OrderVO queryByOrderNo(String orderNo) {
		OrderVO orderVO = new OrderVO();
		orderVO.setOrderNo(orderNo);

		return orderDao.query(orderVO);
	}

	/**
	 * 退货单
	 */
	public boolean refund(Integer orderId, BigDecimal refundAmount, String refundMemo) {
		Order order = new Order();
		order.setId(orderId);
		order.setRefundAmount(refundAmount);
		order.setRefundMemo(refundMemo);

		return orderDao.update(order);
	}

	/**
	 * 新增订单
	 * 
	 * @param goodsList
	 * @return
	 */
	public Integer create(final List<ReqGoods> goodsList, final Integer cityId, final Address address,final Integer cId) {
		getTransactionTemplate().setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
		return getTransactionTemplate().execute(new TransactionCallback<Integer>() {

			@Override
			public Integer doInTransaction(TransactionStatus status) {
				//校验优惠券信息
				Coupon coupon = null;
				if(cId != null && cId > 0){
					coupon = couponService.queryMyCouponDetail(cId);
					if(coupon == null)
						throw new ErrorCodeException(BECode.coupon_8100000);
					
					validateCoupon(coupon, goodsList);
				}
				
				String orderNo = DateUtil.formatDateTime(new Date(), "yyyyMMddHHmmss") + RandomStringUtils.random(4, "1234567890");
				String openId = MemberHelper.getOpenId();
				Integer shopId = MemberHelper.getShopId();
				// 查询买家信息
				Member member = memberService.queryById(openId, shopId);

				Order order = new Order();
				order.setOrderNo(orderNo);
				order.setShopId(MemberHelper.getShopId());
				order.setMemberId(member.getId());
				order.setReceivePhone(address.getPhone());
				order.setReceiveName(address.getName());
				order.setReceiveAddress(address.getProvinceName() + address.getCityName() + address.getAreaName() + address.getAddress());
				order.setNickName(MemberHelper.getNickName());

				// 计算运费，取最大运费为订单运费
				StringBuilder goodsIds = new StringBuilder();
				// 取最大统一运费金额
				List<BigDecimal> transportAmount = new ArrayList<BigDecimal>();

				// 产品总金额（不含运费）
				BigDecimal totalAmount = new BigDecimal("0");
				for (ReqGoods goods : goodsList) {
					// 产品总金额（不含运费）
					totalAmount = totalAmount.add((goods.getPrice().multiply(new BigDecimal(goods.getNum()))));

					if (goods.getUniteTransport() != null) {
						transportAmount.add(goods.getUniteTransport());
					}

					goodsIds.append(goods.getId());
					goodsIds.append(",");

					ShoppingChart chart = new ShoppingChart();
					chart.setGoodsId(goods.getId());
					chart.setMemberId(MemberHelper.getId());
					chart.setIsDeleted("y");
					shoppingChartDao.update("finishGoods", chart);
				}
				Collections.sort(transportAmount);
				// 最大统一金额
				BigDecimal maxAmount = new BigDecimal("0");
				if (!transportAmount.isEmpty()) {
					maxAmount = transportAmount.get(0);
				}

				// 根据运抵城市的id和产品id查询运费模板中的最大运费
				String ids = goodsIds.substring(0, goodsIds.length() - 1);
				List<TranspsortVO> transportList = transportDao.listTransport(ids, cityId);

				List<BigDecimal> amountList = new ArrayList<BigDecimal>();
				for (TranspsortVO tVO : transportList) {
					amountList.add(tVO.getAmount());
				}
				amountList.add(maxAmount);

				if (amountList.isEmpty()) {
					throw new ErrorCodeException(BECode.address_300010);
				}

				Collections.sort(amountList);
				Collections.reverse(amountList);
				maxAmount = amountList.get(0);

				if (maxAmount == null || maxAmount.doubleValue() <= 0) {
					throw new ErrorCodeException(BECode.address_300010);
				}

				order.setTransportAmount(maxAmount);
				order.setAmount(totalAmount.add(maxAmount));
				//校验是否总金额是否满足优惠券的使用限额，不包含运费
				BigDecimal couponAmount = order.getAmount().subtract(order.getTransportAmount());
				if(coupon != null){
					LOG.error("couponAmount " + couponAmount);
					if(coupon.getLimitAmount() != null && couponAmount.doubleValue() < coupon.getLimitAmount()){
						throw new ErrorCodeException(BECode.coupon_8100003);
					}
					
					//设置优惠券相关信息
					order.setCouponId(coupon.getId());
					
					//如果订单金额（不包含运费）小于优惠券的金额，则使用订单金额作为优惠金额
					//如果订单金额大于等于优惠券金额，优惠金额为优惠券面值
					if(couponAmount.doubleValue() < coupon.getAmount()){
						order.setCouponAmount(order.getAmount());
					}else{
						order.setCouponAmount(new BigDecimal(coupon.getAmount()));
					}
					
					//更新会员领取优惠券的状态
					couponService.updateCouponMember(coupon.getId(), MemberHelper.getId(), "1");
					
					//更新优惠券的使用数量
					if(coupon.getAlreadyEmploy() == null){
						coupon.setAlreadyEmploy(1);
					}else{
						coupon.setAlreadyEmploy(coupon.getAlreadyEmploy() + 1);
					}
					
					couponService.update(coupon);
				}
				
				
				Integer orderId = orderDao.create(order);

				for (ReqGoods goods : goodsList) {
					OrderGoods orderGoods = new OrderGoods();
					orderGoods.setOrderId(orderId);
					orderGoods.setGoodsId(goods.getId());
					orderGoods.setGoodsName(goods.getName());
					orderGoods.setImgUrl(goods.getHeadImg());
					orderGoods.setNum(goods.getNum());
					orderGoods.setAmount(goods.getPrice());

					String eId = MemberHelper.getEId();
					// 判断设置提成
					LOG.error("eid:" + eId);
					if (StringUtil.isNotEmpty(eId) && "y".equalsIgnoreCase(goods.getIsPromotion())) {
						ShopExt shopExt = new ShopExt();
						shopExt.setShopId(MemberHelper.getShopId());
						shopExt = (ShopExt) shopExtDao.query("query", shopExt);

						orderGoods.seteId(Integer.parseInt(eId));
						if (goods.getPercentage() != null && goods.getPercentage() > 0) {
							orderGoods.setPercentage(goods.getPercentage());
						} else {
							orderGoods.setPercentage(shopExt.getPercent());
						}
					}

					orderGoodsDao.create(orderGoods);
				}
				//发送定时任务
				addSchedulrTaskForClose(order.getId());
				return orderId;
			}
		});
	}

	/**
	 * 添加订单定时关闭任务
	 */
	private void addSchedulrTaskForClose(Integer orderid){
		
		String cron=DateUtils.dateToCron(DateUtils.dateAfterMin(new Date(), 60));
		String url=GlobEnv.get("scheduler.callback")+"schedulercallback/close_order?orderId="+orderid;
		LOG.info("订单："+orderid+"进入待付款倒计时状态,将于"+cron+"执行！");
		LOG.info("回调地址："+url);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("orderId", orderid);
		LOG.info(String.valueOf(SchedulerHelper.addJobs(url, JSON.toJSONString(map), cron)));
	}
	
	/**
	 * 添加订单定时关闭任务
	 */
	private void removeSchedulrTaskForClose(Integer orderid){
		String url=GlobEnv.get("scheduler_callback")+"schedulercallback/close_order?orderId="+orderid;
		LOG.info("删除定时任务："+url);
		LOG.info(String.valueOf(SchedulerHelper.delJobs(url)));
	}
	/**
	 * 添加订单定时确认收货任务
	 */
//	private void addSchedulrTaskForReceived(Integer orderid){
//		String cron=DateUtils.dateToCron(DateUtils.dateAfterDays(new Date(), 7));
//		String url=GlobEnv.get("scheduler.callback")+"schedulercallback/received_order?orderId="+orderid;
//		LOG.info("订单："+orderid+"进入待确认收货倒计时状态,将于"+cron+"执行！");
//		LOG.info("回调地址："+url);
//		Map<String,Object> map=new HashMap<String,Object>();
//		map.put("orderId", orderid);
//		LOG.info(String.valueOf(SchedulerHelper.addJobs(url, JSON.toJSONString(map), cron)));
//	
//	}
	/**
	 * 分页查询订单产品信息
	 * 
	 * @param goodsId
	 * @param httpObj
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<OrderGoods> listOrderGoods(Integer orderId) {
		OrderGoods orderGoods = new OrderGoods();
		orderGoods.setOrderId(orderId);

		return orderGoodsDao.list(orderGoods);
	}

	/**
	 * 分页查询我的订单信息
	 * 
	 * @param order
	 * @param httpObject
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pages<List<Order>> page(Order order, IHTTPObject httpObject) {

		return (Pages<List<Order>>) orderDao.page(order, httpObject);
	}

	/**
	 * 新增砍价活动订单
	 * 
	 * @param discountId
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public Integer createDisCountOrder(Integer discountId, Goods goods, Address address,Integer cId) {
		//校验优惠券信息
		Coupon coupon = null;
		if(cId != null && cId > 0){
			coupon = couponService.queryMyCouponDetail(cId);
			if(coupon == null)
				throw new ErrorCodeException(BECode.coupon_8100000);
			
			//校验优惠券信息
			validateDiscountCoupon(coupon, goods);
		}
		
		DiscountRecordVO vo = (DiscountRecordVO) discountRecordDao.query("queryById", discountId);

		String orderNo = DateUtil.formatDateTime(new Date(), "yyyyMMddHHmmss") + RandomStringUtils.random(4, "1234567890");
		Integer shopId = MemberHelper.getShopId();
		String openId = MemberHelper.getOpenId();
		// 查询买家信息
		Member member = memberService.queryById(openId, shopId);

		Order order = new Order();
		order.setOrderNo(orderNo);
		order.setShopId(MemberHelper.getShopId());
		order.setMemberId(member.getId());
		order.setReceivePhone(address.getPhone());
		order.setReceiveName(address.getName());
		order.setReceiveAddress(address.getProvinceName() + address.getCityName() + address.getAreaName() + address.getAddress());
		order.setNickName(MemberHelper.getNickName());

		// 如果没有该城市的运费模板，使用统一运费，如果有取运费模板中的运费
		TranspsortVO transVO = transportDao.queryTransportByCity(goods.getId(), address.getCityId());
		if (transVO == null && (goods.getUniteTransport() == null
				|| goods.getUniteTransport().compareTo(new BigDecimal("0")) <= 0)) {
			throw new ErrorCodeException(BECode.order_4000003);
		}

		if (transVO == null) {
			order.setTransportAmount(goods.getUniteTransport());
		} else {
			order.setTransportAmount(transVO.getAmount());
		}

		if (order.getTransportAmount() == null || order.getTransportAmount().doubleValue() <= 0) {
			throw new ErrorCodeException(BECode.address_300010);
		}

		// 总费用为现价+运费
		order.setAmount(vo.getPrice().add(order.getTransportAmount()));
		
		if(coupon != null){
			BigDecimal couponAmount = order.getAmount().subtract(order.getTransportAmount());
			
			//消费金额不足优惠券的使用上限
			if(couponAmount.doubleValue() < coupon.getLimitAmount())
				throw new ErrorCodeException(BECode.coupon_8100003);
			
			order.setCouponId(coupon.getId());
			//订单金额小于优惠金额，使用订单金额，否则使用优惠金额
			if(couponAmount.doubleValue() < coupon.getAmount()){
				order.setCouponAmount(couponAmount);
			}else{
				order.setCouponAmount(new BigDecimal(coupon.getAmount()));
			}
		}
		
		Integer orderId = orderDao.create(order);

		OrderGoods orderGoods = new OrderGoods();
		orderGoods.setAmount(vo.getPrice());
		orderGoods.setOrderId(orderId);
		orderGoods.setGoodsName(goods.getName());
		orderGoods.setGoodsId(goods.getId());
		orderGoods.setImgUrl(goods.getHeadImg());
		orderGoods.setNum(1);

		String eId = MemberHelper.getEId();
		// 判断设置提成
		if (StringUtil.isNotEmpty(eId) && "y".equalsIgnoreCase(goods.getIsPromotion())) {
			ShopExt shopExt = new ShopExt();
			shopExt.setShopId(MemberHelper.getShopId());
			shopExt = (ShopExt) shopExtDao.query("query", shopExt);

			orderGoods.seteId(Integer.parseInt(eId));
			if (goods.getPercentage() != null && goods.getPercentage() > 0) {
				orderGoods.setPercentage(goods.getPercentage());
			} else {
				orderGoods.setPercentage(shopExt.getPercent());
			}
		}

		boolean flag = orderGoodsDao.create(orderGoods) > 0 ? true : false;
		if (flag) {
			// 修改砍价活动未已经下单状态，不可继续下单
			vo.setIssuccess("1");
			discountRecordDao.update(vo);
		}

		ShoppingChart chart = new ShoppingChart();
		chart.setGoodsId(goods.getId());
		chart.setMemberId(MemberHelper.getId());
		chart.setIsDeleted("y");
		shoppingChartDao.update("finishGoods", chart);
		return orderId;
	}

	/**
	 * 统计我的订单
	 * 
	 * @param memberId
	 * @return
	 */
	public OrderVO calc(Integer memberId) {

		return orderDao.calc(memberId);
	}

	/**
	 * 修改为已收货
	 * 
	 * @param order
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean setReceive(Order order) {
		List<OrderGoods> goodsList = listOrderGoods(order.getId());
		for (OrderGoods goods : goodsList) {
			if (goods.geteId() != null && goods.getPercentage() != null) {
				Employee employee = new Employee();
				employee.setId(goods.geteId());
				employee = (Employee) employeeDao.query("queryById", employee);

				EmployeeDeposit deposit = new EmployeeDeposit();
				deposit.setOrderId(order.getId());
				deposit.setOrderNo(order.getOrderNo());
				deposit.setEmployeeId(goods.geteId());
				deposit.setEmployeeName(employee.getName());
				deposit.setGoodName(goods.getGoodsName());
				deposit.setGoodsId(goods.getGoodsId());
				deposit.setPercent(goods.getPercentage());
				deposit.setGoodAmount(goods.getAmount().multiply(new BigDecimal(goods.getNum())));
				deposit.setAmount(deposit.getGoodAmount()
						.multiply(new BigDecimal(deposit.getPercent()).divide(new BigDecimal("100"))));

				employeeDepositDao.insert(deposit);

				if (employee.getTotalAmount() == null) {
					employee.setTotalAmount(deposit.getAmount());
				} else {
					employee.setTotalAmount(employee.getTotalAmount().add(deposit.getAmount()));
				}
				employeeDao.update(employee);
			}

			Goods gg = goodsDao.query(goods.getGoodsId());
			if (gg.getTotalNum() == null) {
				gg.setTotalNum(1);
			} else {
				gg.setTotalNum(gg.getTotalNum() + 1);
			}
			goodsDao.update(gg);

		}

		Member member = new Member();
		member.setId(order.getMemberId());
		member.setShopId(order.getShopId());
		
		member = memberService.query(member);
		member.setLastOrderTime(order.getGmtModified());
		if (member.getPurchaseAmount() == null) {
			member.setPurchaseAmount(order.getAmount());
		} else {
			member.setPurchaseAmount(member.getPurchaseAmount().add(order.getAmount()));
		}
		memberService.update(member);

		WxMember wxMember = new WxMember();
		wxMember.setWxOpenId(MemberHelper.getOpenId());
		wxMember.setShopId(order.getShopId());
		wxMember = (WxMember) wxMemberDao.query("query", wxMember);

		if (wxMember != null) {
			if (wxMember.getBuyCount() == null) {
				wxMember.setBuyCount(1);
			} else {
				wxMember.setBuyCount(wxMember.getBuyCount() + 1);
			}

			if (wxMember.getTotalAmount() == null) {
				wxMember.setTotalAmount(order.getAmount());
			} else {
				wxMember.setTotalAmount(wxMember.getTotalAmount().add(order.getAmount()));
			}
			wxMember.setLastOrderTime(new Date());
			wxMemberDao.update(wxMember);
		}

		order.setStatus("3");
		return orderDao.update(order);
	}

	public static HttpServletRequest getRequest() {

		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	/**
	 * 支付回调
	 * 
	 * @param order
	 * @param goodsList
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean changeStatus(Order order, List<OrderGoods> goodsList,Shop shop) {
		// 设置订单状态为待发货
		order.setStatus("1");
		orderDao.update(order);

		for (OrderGoods orderGoods : goodsList) {
			Goods goods = goodsDao.query(orderGoods.getGoodsId());
			goods.setInventory(goods.getInventory() - orderGoods.getNum());
			goodsDao.updateInventory(goods);
		}

		// 新增订单流水
		Deposit deposit = new Deposit();
		deposit.setOrderNo(order.getOrderNo());
		deposit.setDepositNo(DateUtil.formatDateTime(new Date(), "yyyyMMddHHmmss") + RandomStringUtils.random(4, "1234567890"));
		deposit.setBuyyerId(order.getMemberId());
		deposit.setBuyyerName(order.getNickName());
		deposit.setTradeId(order.getShopId());
		deposit.setTradeName(shop.getCompanyName());
		deposit.setAmount(order.getAmount());
		deposit.setType("0");
		deposit.setCategory("0");
		deposit.setMemo(order.getMemo());
		depositDao.insert(deposit);
		//删除定时任务
		removeSchedulrTaskForClose(order.getId());
		return true;
	}
	
	/**
	 * 校验优惠券信息
	 * @param couponId
	 * @param goodsList
	 */
	private void validateCoupon(Coupon coupon,List<ReqGoods> goodsList){
		if(coupon.getGmtEnd().compareTo(new Date()) < 0)
			throw new ErrorCodeException(BECode.coupon_8100005);
		
		//如果优惠券仅限部分产品
		if("n".equalsIgnoreCase(coupon.getEmployScope())){
			CouponGoods couponGoods = couponService.queryCouponGoods(coupon.getId());
				
			if(couponGoods == null)
				throw new ErrorCodeException(BECode.coupon_8100002);
				
			Map<Integer,ReqGoods> goodsMap = new HashMap<Integer,ReqGoods>();
			for(ReqGoods goods : goodsList){
				goodsMap.put(goods.getId(), goods);
			}
				
			//校验指定产品的优惠券是否在使用范围内
			ReqGoods goods = goodsMap.get(couponGoods.getGoodsId());
			if(goods == null)
				throw new ErrorCodeException(BECode.coupon_8100001);
		}
		
	}
	
	/**
	 * 校验优惠券信息
	 * @param couponId
	 * @param goodsList
	 */
	private void validateDiscountCoupon(Coupon coupon,Goods goods){
		if(coupon.getGmtEnd().compareTo(new Date()) < 0)
			throw new ErrorCodeException(BECode.coupon_8100005);
		
		//如果优惠券仅限部分产品
		if("n".equalsIgnoreCase(coupon.getEmployScope())){
			CouponGoods couponGoods = couponService.queryCouponGoods(coupon.getId());
				
			if(couponGoods == null)
				throw new ErrorCodeException(BECode.coupon_8100002);
				
			//校验指定产品的优惠券是否在使用范围内
			if(goods.getId() != couponGoods.getGoodsId())
				throw new ErrorCodeException(BECode.coupon_8100001);
		}
		
	}
}
